package net.muchine.core.service.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import net.muchine.core.dao.user.UserDao;
import net.muchine.core.domain.user.User;
import net.muchine.core.domain.user.UserSearch;
import net.muchine.core.service.GenericServiceImpl;
import net.muchine.core.service.user.exception.PasswordNotMatchedException;
import net.muchine.core.service.user.exception.UserAccountNotExistException;
import net.muchine.core.support.util.Validator;

@Component
public class UserServiceImpl extends GenericServiceImpl<User, Long>
		implements UserService {

	@Autowired Validator validator;
	@Autowired UserDao dao;

	@Override
	public void setGenericDao() {
		super.setDao(this.dao);
	}

	public User login(String userid, String password)
			throws UserAccountNotExistException, PasswordNotMatchedException {
		if (validator.isEmptyString(userid)) 
			throw new UserAccountNotExistException();
		if (validator.isEmptyString(password))
			throw new PasswordNotMatchedException();
		
		User user = dao.findById(userid);
		if (user == null) 
			throw new UserAccountNotExistException(userid);
		if (!password.equals(user.getPassword())) 
			throw new PasswordNotMatchedException(userid);
		
		return user;
	}

	public List<User> findByParameters(UserSearch search) {
		return dao.findByParameters(search);
	}
	
	public User findById(String id) {
		return dao.findById(id);
	}

	public List<User> selectAll() {
		return dao.selectAll();
	}
}
